<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="项目号" prop="projectNumber">
        <el-input
          v-model="queryParams.projectNumber"
          placeholder="请输入项目号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="公司名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="注册园区及平台" prop="registrationPark">
        <el-input
          v-model="queryParams.registrationPark"
          placeholder="请输入注册园区及平台"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="法人" prop="legalPerson">
        <el-input
          v-model="queryParams.legalPerson"
          placeholder="请输入法人"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="法人实名电话号码" prop="legalPersonPhone">
        <el-input
          v-model="queryParams.legalPersonPhone"
          placeholder="请输入法人实名电话号码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="企业联系人" prop="enterpriseContact">
        <el-input
          v-model="queryParams.enterpriseContact"
          placeholder="请输入企业联系人"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="企业联系人电话" prop="enterpriseContactPhone">
        <el-input
          v-model="queryParams.enterpriseContactPhone"
          placeholder="请输入企业联系人电话"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="纳税人属性" prop="taxpayerAttribute">
        <el-input
          v-model="queryParams.taxpayerAttribute"
          placeholder="请输入纳税人属性"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="收费" prop="fee">
        <el-input
          v-model="queryParams.fee"
          placeholder="请输入收费"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="招商负责人" prop="investmentLeader">
        <el-input
          v-model="queryParams.investmentLeader"
          placeholder="请输入招商负责人"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="介绍人或下级平台" prop="introducer">
        <el-input
          v-model="queryParams.introducer"
          placeholder="请输入介绍人或下级平台"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="税收情况" prop="taxReformSituation">
        <el-input
          v-model="queryParams.taxReformSituation"
          placeholder="请输入税收情况"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="是否为简化企业" prop="isSimplifiedEnterprise">
        <el-select
          v-model="queryParams.isSimplifiedEnterprise"
          placeholder="请选择是否为简化企业"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否为重点企业" prop="isKeyEnterprises">
        <el-select
          v-model="queryParams.isKeyEnterprises"
          placeholder="请选择是否为重点企业"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="返税比例" prop="taxRebateRatio">
        <el-input
          v-model="queryParams.taxRebateRatio"
          placeholder="请输入返税比例"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="项目引进情况说明" prop="projectIntroduction">
        <el-input
          v-model="queryParams.projectIntroduction"
          placeholder="请输入项目引进情况说明"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="审批结果" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择审批结果"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.BPM_PROCESS_INSTANCE_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="流程实例的编号" prop="processInstanceId">
        <el-input
          v-model="queryParams.processInstanceId"
          placeholder="请输入流程实例的编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['bpm:business-project:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['bpm:business-project:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="项目主键" align="center" prop="id" />
      <el-table-column label="项目号" align="center" prop="projectNumber" />
      <el-table-column label="公司名称" align="center" prop="companyName" />
      <el-table-column label="项目类型：1新注册 2代理记账 3迁移" align="center" prop="projectType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.BPM_PROJECT_TYPE" :value="scope.row.projectType" />
        </template>
      </el-table-column>
      <el-table-column label="注册园区及平台" align="center" prop="registrationPark" />
      <el-table-column label="法人" align="center" prop="legalPerson" />
      <el-table-column label="法人实名电话号码" align="center" prop="legalPersonPhone" />
      <el-table-column label="企业联系人" align="center" prop="enterpriseContact" />
      <el-table-column label="企业联系人电话" align="center" prop="enterpriseContactPhone" />
      <el-table-column label="纳税人属性" align="center" prop="taxpayerAttribute" />
      <el-table-column label="收费" align="center" prop="fee" />
      <el-table-column label="招商负责人" align="center" prop="investmentLeader" />
      <el-table-column label="介绍人或下级平台" align="center" prop="introducer" />
      <el-table-column label="税收情况" align="center" prop="taxReformSituation" />
      <el-table-column label="是否为简化企业" align="center" prop="isSimplifiedEnterprise">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.isSimplifiedEnterprise" />
        </template>
      </el-table-column>
      <el-table-column label="是否为重点企业" align="center" prop="isKeyEnterprises">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.isKeyEnterprises" />
        </template>
      </el-table-column>
      <el-table-column label="返税比例" align="center" prop="taxRebateRatio" />
      <el-table-column label="项目引进情况说明" align="center" prop="projectIntroduction" />
      <el-table-column label="审批结果" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.BPM_PROCESS_INSTANCE_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="流程实例的编号" align="center" prop="processInstanceId" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['bpm:business-project:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['bpm:business-project:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <BusinessProjectForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getIntDictOptions, getBoolDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { BusinessProjectApi, BusinessProjectVO } from '@/api/bpm/businessproject'
import BusinessProjectForm from './BusinessProjectForm.vue'

/** 引入项目审核信息 列表 */
defineOptions({ name: 'BusinessProject' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<BusinessProjectVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  projectNumber: undefined,
  companyName: undefined,
  projectType: undefined,
  registrationPark: undefined,
  legalPerson: undefined,
  legalPersonPhone: undefined,
  enterpriseContact: undefined,
  enterpriseContactPhone: undefined,
  taxpayerAttribute: undefined,
  fee: undefined,
  investmentLeader: undefined,
  introducer: undefined,
  taxReformSituation: undefined,
  isSimplifiedEnterprise: undefined,
  isKeyEnterprises: undefined,
  taxRebateRatio: undefined,
  projectIntroduction: undefined,
  status: undefined,
  processInstanceId: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await BusinessProjectApi.getBusinessProjectPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await BusinessProjectApi.deleteBusinessProject(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await BusinessProjectApi.exportBusinessProject(queryParams)
    download.excel(data, '引入项目审核信息.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>